/// grass/pages/grass_topic/grass_topic.js
var app = getApp();

var common = require("../../../common/common.js").common;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        chooseClass: 1,
        //1,话题热榜 2，点赞排行榜
        thumbsList: [],
        page: 1,
        rows: 10
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        if (options.status) {
            that.setData({
                chooseClass: options.status
            });
        }
        that.requestStart();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.setData({
            page: 1,
            thumbsList: []
        });
        that.requestStart();
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getRank();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    requestStart: function() {
        let that = this;
        that.getRank();
        that.getBase();
    },
    //全局基础配置信息
    getBase: function(openid) {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    color_header: res.extra.color_header,
                    color_text: res.extra.color_text,
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    style: res.extra.style
                });
            }
        });
    },
    chooseClass: function(e) {
        let that = this;
        let chooseClass = e.currentTarget.dataset.id;
        that.setData({
            page: 1,
            thumbsList: [],
            chooseClass: chooseClass
        });
        that.getRank();
    },
    getRank: function() {
        let that = this;
        common.request({
            endpoint: "Plantgrass/list/rank",
            method: "post",
            data: {
                do: that.data.chooseClass == 1 ? "topic" : "praise",
                page: that.data.page,
                rows: that.data.rows
            },
            success: res => {
                if (res.rows.length) {
                    let thumbsList = res.rows;
                    if (that.data.page != 1) {
                        thumbsList = that.data.thumbsList.concat(thumbsList);
                    }
                    that.setData({
                        thumbsList: thumbsList,
                        page: that.data.page + 1
                    });
                }
            }
        });
    },
    /**
   * 跳转视频或者图文详情页
   */
    toDetail: function(e) {
        let type = e.currentTarget.dataset.item.type;
        if (type == 1) {
            //图文
            wx.navigateTo({
                url: "/grass/pages/grassDetail/grassDetail?id=" + e.currentTarget.dataset.item.id
            });
        } else {
            wx.navigateTo({
                url: "/grass/pages/grassVideo/grassVideo?id=" + e.currentTarget.dataset.item.id
            });
        }
    },
    toDetailIndex: function(e) {
        let that = this;
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        let topicList = [ e.currentTarget.dataset.item ];
        prevPage.setData({
            page: 1,
            chooseTopic: e.currentTarget.dataset.item.id,
            topicList: topicList
        });
        prevPage.dealTopList();
        prevPage.getNewGrass();
        wx: wx.navigateBack({
            //返回上一个页面
            delta: 1
        });
    }
});